package com.biz.hunter.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biz.hunter.dao.CompanyDAO;
import com.biz.hunter.dao.DomainDAO;
import com.biz.hunter.dao.EmployeeDAO;
import com.biz.hunter.dao.PersonDAO;
import com.biz.hunter.db.entity.Company;
import com.biz.hunter.db.entity.Domain;
import com.biz.hunter.db.entity.Employee;
import com.biz.hunter.db.entity.Person;
import com.biz.hunter.service.IEmployeeService;

@Service
@Transactional
public class EmployeeService implements IEmployeeService {
  private static Logger logger = LoggerFactory.getLogger(EmployeeService.class);

  @Autowired
  private EmployeeDAO empDAO;

  @Autowired
  private PersonDAO personDAO;

  @Autowired
  private CompanyDAO compDAO;
  @Autowired
  private DomainDAO domainDAO;

  
  @Override
  public Employee getEmployeeByID(Integer empId) {
    Employee emp = empDAO.getById(empId);
    return emp;
  }

  @Override
  public List<Employee> getAllEmployees() {
    return empDAO.getALLEmps();
  }

  @Override
  public int createEmployee(Employee emp) {
//    Person person = (Person)emp;
    
    //TODO change phone as the key of person
	Person person = personDAO.getByPhone(emp.getPhone());
    int personId = -1;
    if (person == null) {
      int id = personDAO.save(person);
      personId = id;
      logger.debug("saved id is " + id);
    } else {
      int id = personDAO.update(person);
      personId = person.getId();
      logger.debug("update person id is " + id);
    }

    // person = personDAO.getByPhone(emp.getPhone());
    // if(person != null)
    if (personId > 0) {
      emp.setPersonId(personId);
      return empDAO.update(emp);
    } else {
      return -1;
    }
  }

  @Override
  public void deleteById(Integer Id) {
    if (Id != null)
      empDAO.delete(Id.toString());
  }

  @Override
  public void updateEmployee(Person person,Employee emp) {
    personDAO.update(person);
    empDAO.update(emp);
  }

  @Override
  public Employee getByOpenId(String openid) {
    Employee emp = empDAO.getByOpenId(openid);
    return emp;
  }

  @Override
  public Employee getEmpByPhone(String phone, String openId) {
    return empDAO.getbyPhone(phone, openId);
  }

  @Override
  public void saveSecureCode(Employee emp) {
	Person person=personDAO.getById(emp.getPersonId());
    int id = personDAO.save(person);
    emp.setPersonId(id);
    empDAO.saveSecureCode(emp);
  }

  @Override
  public long bindedPhone(Employee emp) {
//    emp.setName("temp");
//    personDAO.update(emp);
    return empDAO.bindedPhone(emp);
  }

  @Override
  public void updateEmployeeProfile(Person person,Employee emp) {

    personDAO.updateProfile(person);
//    int compId = emp.getCompanyId();
//    Company comp = compDAO.getById(compId);
//    if(comp != null)
//    {
//      int domainId = comp.getDomain();
//      emp.setDomainId(domainId);
//      Domain dn = domainDAO.getById(domainId);
//      if(dn!=null)
//      {
//        emp.setDomainName(dn.getName());
//      }
//    }
    empDAO.updateDomain(emp);
//    empDAO.updateProfile(emp);
  }

  @Override
  public int updateExpectInfo(Employee emp) {
//    personDAO.update(sessionEmp);
//    empDAO.updateDomain(emp);
    return  empDAO.updateProfile(emp);
    
   
  }

}
